package com.ranshiyan.serviceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ranshiyan.dao.ArticleMapper;
import com.ranshiyan.entity.Article;
import com.ranshiyan.entity.Comment;
import com.ranshiyan.service.ArticleService;
import com.ranshiyan.utils.CmsContant;

@Service
public class ArticleServiceImpl implements ArticleService{

	@Autowired
	ArticleMapper articleMapper;
	
	@Override
	public PageInfo list(int page) {
		// TODO Auto-generated method stub
		PageHelper.startPage(page,CmsContant.PAGE_SIZE);
		return new PageInfo<Article>(articleMapper.list());
	}

	
	@Override
	public Article getById(int id) {
		// TODO Auto-generated method stub
		return articleMapper.getById(id);
	}


	@Override
	public int addComment(int articleId, String content, Integer userId) {
		// TODO Auto-generated method stub
		Comment comment = new Comment();
		comment.setArticleId(articleId);
		comment.setContent(content);
		comment.setUserId(userId);
		int addCnt = articleMapper.addComment(comment);
		if(addCnt>0) {
			articleMapper.increaseCommentCnt(articleId);//评论的数量自增1
		}
		return addCnt;
	}


	@Override
	public PageInfo<Comment> listComment(int articleId, int page) {
		// TODO Auto-generated method stub
		PageHelper.startPage(page, CmsContant.PAGE_SIZE);
		return new PageInfo<Comment>(articleMapper.listCommentByArticleId(articleId));
	}

}
